{                                 
Written by Daniel Harris, with help from Sensiron Demo files

A short demo for the prop pro development board to demo the sensiron temperature and humidity sensor.
This program reads temp and humidity from the sensor and displays this on the LED alpha displays on the
top of the board.

To get the displays working, jump pins directly from the prop to the segment pins on the display.  This
demo only uses two of the three displays.  Segment A on the display should connect to pin 0 on the prop.
Segment G should be on pin 6, and the other segments should be connected inclusively.  Since these
displays are multiplexed, we need to tell which display should be turned on.  Connect the left displays
left number to pin 7, and the right displays right number to pin 10.  Connect the other two inclusively
as well.  Once this is done, the left display will display temp and the right will display humidity. 
}

CON

  'setup the system clock
  '5 MHz input frequency * 16x PLL multiplier = 80 MHz per core
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


  _0 = %0011_1111
  _1 = %0000_0110
  _2 = %0101_1011
  _3 = %0100_1111
  _4 = %0110_0110
  _5 = %0110_1101
  _6 = %0111_1101
  _7 = %0000_0111
  _8 = %0111_1111
  _9 = %0110_1111
  _A = %0111_0111
  _B = %0111_1100
  _C = %0011_1001
  _D = %0101_1110
  _E = %0111_1001
  _F = %0111_0001

  LEDA_PIN = 0
  LEDG_PIN = 6

  LL_PIN = 7
  LR_PIN = 8
  RL_PIN = 9
  RR_PIN = 10

  LL = %0111
  LR = %1011
  RL = %1101
  RR = %1110

  SHT_DATA = 18
  SHT_CLOCK = 19


obj

  sht           : "Sensirion"
  f             : "FloatMath"

var

  long temp_stack[50]
  long rawHumidity
  long rawTemp
  long tempC, tempF, rel_hum


pub start

  cognew(read_temp, @temp_stack)

  dira[LEDA_PIN..LEDG_PIN]~~
  dira[LL_PIN..RR_PIN]~~
  
  repeat
    'rawTemp := f.FFloat(sht.readTemperature)
    'rawHumidity := f.FFloat(sht.readHumidity)
     
    
    outa[LL_PIN..RR_PIN] := LL		'select the left left display and print
    outa[LEDG_PIN..LEDA_PIN] := dec2led(f.FRound(tempF)/10)
    outa[LEDG_PIN..LEDA_PIN]~

    outa[LL_PIN..RR_PIN] := LR
    outa[LEDG_PIN..LEDA_PIN] := dec2led(f.FRound(tempF)//10)
    outa[LEDG_PIN..LEDA_PIN]~
    
    outa[LL_PIN..RR_PIN] := RL
    outa[LEDG_PIN..LEDA_PIN] := dec2led(f.FRound(rel_hum)/10)
    outa[LEDG_PIN..LEDA_PIN]~
    
    outa[LL_PIN..RR_PIN] := RR
    outa[LEDG_PIN..LEDA_PIN] := dec2led(f.FRound(rel_hum)//10)
    outa[LEDG_PIN..LEDA_PIN]~


pub read_temp

  sht.start(SHT_DATA, SHT_CLOCK)                          ' start sensirion object

  repeat
    rawHumidity := f.FFloat(sht.readHumidity)
    rawTemp := f.FFloat(sht.readTemperature)

    tempC := celsius(rawTemp)
    rel_hum := humidity(tempC, rawHumidity)
    
    tempF := fahrenheit(tempC)
    
    

PUB celsius(t)
  ' from SHT1x/SHT7x datasheet using value for 3.5V supply
  ' celsius = -39.66 + (0.01 * t)
  return f.FAdd(-39.66, f.FMul(0.01, t)) 

PUB fahrenheit(t)
  ' fahrenheit = (celsius * 1.8) + 32
  return f.FAdd(f.FMul(t, 1.8), 32.0)
  
PUB humidity(t, rh) | rhLinear
  ' rhLinear = -4.0 + (0.0405 * rh) + (-2.8e-6 * rh * rh)
  ' simplifies to: rhLinear = ((-2.8e-6 * rh) + 0.0405) * rh -4.0
  rhLinear := f.FAdd(f.FMul(f.FAdd(0.0405, f.FMul(-2.8e-6, rh)), rh), -4.0)
  ' rhTrue = (t - 25.0) * (0.01 + 0.00008 * rawRH) + rhLinear
  return f.FAdd(f.FMul(f.FSub(t, 25.0), f.FAdd(0.01, f.FMul(0.00008, rh))), rhLinear)


pri dec2led(dec)

  case dec
    0:
      return _0
    1:
      return _1
    2:
      return _2
    3:
      return _3
    4:
      return _4
    5:
      return _5
    6:
      return _6
    7:
      return _7
    8:
      return _8
    9:
      return _9
    10:
      return _A
    11:
      return _B
    12:
      return _C
    13:
      return _D
    14:
      return _E
    15:
      return _F
    Other:
      return _F
  

                                                                                     
